package com.agileai.pm.module.proc.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProcWeeklyManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.util.StringUtil;

public class ProcWeeklyManageEditHandler
        extends StandardEditHandler {
    public ProcWeeklyManageEditHandler() {
        super();
        this.listHandlerClass = ProcWeeklyManageListHandler.class;
        this.serviceId = buildServiceId(ProcWeeklyManage.class);
    }

    protected void processPageAttributes(DataParam param) {
      
    }
    
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (operaType.equals("again")){
			Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
    		param.put("projectId", project.getProjectId());
    		param.put("projectName", project.getProjectName());
    		param.put("limitNum", 3);
    		List<DataRow> weeklyList =  this.getService().findRecords(param);
    		DataRow thisWeeklyInfo = weeklyList.get(0);
    		String thisWeeklyStartTime = thisWeeklyInfo.stringValue("PWI_START_TIME");
    		String thisWeeklyEndTime = thisWeeklyInfo.stringValue("PWI_END_TIME");
    		String nextWeeklyStartTime = thisWeeklyInfo.stringValue("PWI_NEXT_WEEK_START");
    		String nextWeeklyEndTime = thisWeeklyInfo.stringValue("PWI_NEXT_WEEK_END");
    		String followWeeklyStartTime = thisWeeklyInfo.stringValue("PWI_FOLLOW_WORK_START");
    		String followWeeklyEndTime = thisWeeklyInfo.stringValue("PWI_FOLLOW_WORK_END");
    		this.setAttribute("PWI_START_TIME", thisWeeklyStartTime);
    		this.setAttribute("PWI_END_TIME", thisWeeklyEndTime);
    		this.setAttribute("PWI_NEXT_WEEK_START", nextWeeklyStartTime);
    		this.setAttribute("PWI_NEXT_WEEK_END", nextWeeklyEndTime);
    		this.setAttribute("PWI_FOLLOW_WORK_START", followWeeklyStartTime);
    		this.setAttribute("PWI_FOLLOW_WORK_END", followWeeklyEndTime);
    		
    		this.setAttribute("PWI_EMAILL_THEME", thisWeeklyInfo.stringValue("PWI_EMAILL_THEME"));
    		this.setAttribute("PWI_ID", thisWeeklyInfo.stringValue("PWI_ID"));
    		if(weeklyList.size()>1){
    			DataRow lastWeeklyInfo = weeklyList.get(1);
    			this.setAttribute("lastWeeklyEndTime", lastWeeklyInfo.stringValue("PWI_END_TIME"));
    		}
    		
		}
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    
    
  @PageAction
  public ViewRenderer weeklyExtraction(DataParam param){
	  String responseText="true";
	  boolean judgeState = true;
	  String operaType = param.get(OperaType.KEY);
    	try {
    		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
    		param.put("projectId", project.getProjectId());
    		param.put("projectName", project.getProjectName());
    		param.put("limitNum", 3);
    		List<DataRow> weeklyList =  this.getService().findRecords(param);
    		String lastWeekEndTime = "";
    		if(weeklyList.size()>0){
    			lastWeekEndTime=weeklyList.get(0).stringValue("PWI_END_TIME");
    		}
    		
    		if (operaType.equals("again")){
    			lastWeekEndTime = param.stringValue("lastWeeklyEndTime");
    		}
    		
    		String thisWeeklyStartTime = param.stringValue("PWI_START_TIME");
    		String thisWeeklyEndTime = param.stringValue("PWI_END_TIME");
    		String nextWeeklyStartTime = param.stringValue("PWI_NEXT_WEEK_START");
    		String nextWeeklyEndTime = param.stringValue("PWI_NEXT_WEEK_END");
    		String followWeeklyStartTime = param.stringValue("PWI_FOLLOW_WORK_START");
    		String followWeeklyEndTime = param.stringValue("PWI_FOLLOW_WORK_END");
    		
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		
    		if(StringUtil.isNotNullNotEmpty(lastWeekEndTime)){
    			if(sdf.parse(thisWeeklyStartTime).getTime()<sdf.parse(lastWeekEndTime).getTime()&&judgeState){
        			responseText="总结开始时间必须大于上周结束时间" ;
        			judgeState = false;
        		}
    		}
    		
    		if(sdf.parse(thisWeeklyEndTime).getTime()<sdf.parse(thisWeeklyStartTime).getTime()&&judgeState){
    			responseText="总结结束时间必须大于开始时间" ;
    			judgeState = false;
    		}
    		if(sdf.parse(nextWeeklyStartTime).getTime()<sdf.parse(nextWeeklyStartTime).getTime()&&judgeState){
    			responseText="下周计划开始时间必须大于总结结束时间" ;
    			judgeState = false;
    		}
    		if(sdf.parse(nextWeeklyEndTime).getTime()<sdf.parse(nextWeeklyStartTime).getTime()&&judgeState){
    			responseText="下周计划结束必须大于下周计划开始时间" ;
    			judgeState = false;
    		}
    		if(sdf.parse(followWeeklyStartTime).getTime()<sdf.parse(nextWeeklyEndTime).getTime()&&judgeState){
    			responseText="后续计划开始时间必须大于下周计划结束时间" ;
    			judgeState = false;
    		}
    		if(sdf.parse(followWeeklyEndTime).getTime()<sdf.parse(followWeeklyStartTime).getTime()&&judgeState){
    			responseText="后续计划结束时间必须大于后续计划开始时间" ;
    			judgeState = false;
    		}
    		if(judgeState){
    			this.insertWeeklyInfos(param);
    		}
    		
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return new AjaxRenderer(responseText);
    	
	}
  
    protected void insertWeeklyInfos(DataParam param){
    	String operaType = param.get(OperaType.KEY);
    	String weeklyId = KeyGenerator.instance().genKey();
    	if (operaType.equals("again")){
    		weeklyId = param.stringValue("PWI_ID");
			this.getService().deletWeeklyRecord(weeklyId);
	    	String trackId = this.getService().getTrackId(weeklyId);
	    	if(StringUtil.isNotNullNotEmpty(trackId)){
	    		getService().delTrackInfo(trackId);
	    	}
		}
    	
    	String projectId = param.getString("projectId");
    	String projectName = param.getString("projectName");
    	
    	DataParam weeklyInfoParam = new DataParam();
    	weeklyInfoParam.put("PPA_ID", projectId);
    	weeklyInfoParam.put("PWI_NAME", projectName+"&nbsp;"+param.stringValue("PWI_START_TIME")+"至"+param.stringValue("PWI_END_TIME")+"&nbsp;周报");
    	weeklyInfoParam.put("PWI_STATE", "");
    	weeklyInfoParam.put("PWI_EMAILL_THEME", param.stringValue("PWI_EMAILL_THEME"));
    	weeklyInfoParam.put("PWI_LIMIT", 0);
    	weeklyInfoParam.put("PWI_START_TIME", param.stringValue("PWI_START_TIME"));
    	weeklyInfoParam.put("PWI_END_TIME", param.stringValue("PWI_END_TIME"));
    	weeklyInfoParam.put("PWI_NEXT_WEEK_START", param.stringValue("PWI_NEXT_WEEK_START"));
    	weeklyInfoParam.put("PWI_NEXT_WEEK_END", param.stringValue("PWI_NEXT_WEEK_END"));
    	weeklyInfoParam.put("PWI_FOLLOW_WORK_START", param.stringValue("PWI_FOLLOW_WORK_START"));
    	weeklyInfoParam.put("PWI_FOLLOW_WORK_END", param.stringValue("PWI_FOLLOW_WORK_END"));
    	this.getService().createRecord(weeklyInfoParam, weeklyId);
    	
    	DataParam conditionsParam = new DataParam();
    	conditionsParam.put("projectId",projectId);
    	
    	conditionsParam.put("startTime", param.stringValue("PWI_START_TIME"));
    	conditionsParam.put("endTime", param.stringValue("PWI_END_TIME"));
    	List<DataRow> thisWeeklyInfo = this.getService().findPlanreRecords(conditionsParam);
    	List<DataParam>  thisWeeklyDetailed = this.structuralData(thisWeeklyInfo, weeklyId, "THIS_WEEKLY");
    	
    	conditionsParam.put("startTime", param.stringValue("PWI_NEXT_WEEK_START"));
    	conditionsParam.put("endTime", param.stringValue("PWI_NEXT_WEEK_END"));
    	List<DataRow> nextWeeklyInfo = this.getService().findPlanreRecords(conditionsParam);
    	List<DataParam>  nextWeeklyDetailed = this.structuralData(nextWeeklyInfo, weeklyId, "NEXT_WEEKLY");
    	
    	conditionsParam.put("startTime", param.stringValue("PWI_FOLLOW_WORK_START"));
    	conditionsParam.put("endTime", param.stringValue("PWI_FOLLOW_WORK_END"));
    	List<DataRow> followWeeklyInfo = this.getService().findPlanreRecords(conditionsParam);
    	List<DataParam> followWeeklyDetailed = this.structuralData(followWeeklyInfo, weeklyId, "FOLLOW_WEEKLY");
    	
    	List<DataParam> saveDataList = new ArrayList<DataParam>();
    	saveDataList.addAll(thisWeeklyDetailed);
    	saveDataList.addAll(nextWeeklyDetailed);
    	saveDataList.addAll(followWeeklyDetailed);
    	
    	this.getService().insertWeeklyDetailed(saveDataList);
    }


    protected List<DataParam> structuralData(List<DataRow> list,String weeklyId,String detailedState) {
	    List<DataParam> resultList = new ArrayList<DataParam>();
	    for(int i = 0;i<list.size();i++){
    	    DataRow row = list.get(i);
    	    Float PWD_WORKLOAD = null;
    	    if(StringUtil.isNotNullNotEmpty(row.stringValue("PPA_WORK_DAY"))){
    	    	PWD_WORKLOAD = Float.parseFloat(row.stringValue("PPA_WORK_DAY"));
    	    }
    	    
    		DataParam weeklyDetailed = new DataParam();
    		weeklyDetailed.put("PWD_ID", KeyGenerator.instance().genKey());
    		weeklyDetailed.put("PWI_ID", weeklyId);
    		weeklyDetailed.put("PWD_TYPE", detailedState);
    		weeklyDetailed.put("PWD_CONTENT", row.stringValue("PPA_CONTENT"));
    		weeklyDetailed.put("PWD_CHARGE", row.stringValue("PPA_CHARGE"));
    		weeklyDetailed.put("PWD_WORKLOAD", PWD_WORKLOAD );
    		weeklyDetailed.put("PWD_SATRT_TIME", row.stringValue("PPA_START_TIME"));
    		weeklyDetailed.put("PWD_STATE", row.stringValue("PPA_STATE"));
    		weeklyDetailed.put("PWD_EXPLAIN","");
    		resultList.add(weeklyDetailed);
	    }
	    
	    if(list.size()==0){
	    	DataParam weeklyDetailed = new DataParam();
    		weeklyDetailed.put("PWD_ID", KeyGenerator.instance().genKey());
    		weeklyDetailed.put("PWI_ID", weeklyId);
    		weeklyDetailed.put("PWD_TYPE", detailedState);
    		weeklyDetailed.put("PWD_CONTENT","");
    		weeklyDetailed.put("PWD_CHARGE", "");
    		weeklyDetailed.put("PWD_WORKLOAD",null);
    		weeklyDetailed.put("PWD_SATRT_TIME", null);
    		weeklyDetailed.put("PWD_STATE", "");
    		weeklyDetailed.put("PWD_EXPLAIN","");
    		resultList.add(weeklyDetailed);
	    }
	    
	   return resultList;
    }
    
   protected ProcWeeklyManage getService() {
       return (ProcWeeklyManage) this.lookupService(this.getServiceId());
    }
}
